package cj.com.chabaike.ui.act.LonginAndJoin;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;
import cj.com.chabaike.R;
import cj.com.chabaike.bll.PandaPagerManager;
import cj.com.chabaike.bll.RWServer;
import cj.com.chabaike.dao.FetchJSONDAO;
import cj.com.chabaike.entity.seach.SeachData;
import cj.com.chabaike.entity.seach.Seach_ContentList;
import cj.com.chabaike.ui.act.MainPager;
import cj.com.chabaike.ui.adapter.BaseRecyclerAdapter;
import cj.com.chabaike.ui.utils.HttpBitMapHelp;
import cj.com.chabaike.ui.utils.Navigator;

/**
 * Created by bingo on 2015/8/13.
 */
public class SearchPager extends ActionBarActivity implements View.OnClickListener {


    /**
     * ***********************全局变量区**************************************
     */
    RecyclerView recyclerView;
    private MainPager.DrawerToggle mSlider; //三道杠
    private List<Seach_ContentList> Seach_ContentListlist;//Recycler数据源
    private BitmapUtils bitmapUtils;
    SeachDataRecyclerAdapter seachDataRecyclerAdapter;//适配器
    Button seach_pager_submitBtn; //搜索按钮
    EditText seach_pager_editText;
    RWServer rws;
    PandaPagerManager ppm;


    /**************************全局变量区结束***************************************/
    /**
     * ***********************生命周期方法区**************************************
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_layout);
        seach_pager_submitBtn = (Button) findViewById(R.id.seach_pager_submitBtn);
        seach_pager_submitBtn.setOnClickListener(this);
        seach_pager_editText = (EditText) findViewById(R.id.seach_pager_editText);
        initToolbar();
        initRecyclerView();
        rws = RWServer.getInstance(getApplicationContext());
        ppm = PandaPagerManager.getPandaPagerManagerInstance();
    }
    /**************************生命周期方法区结束***************************************/
    /**
     * ***********************init区**************************************
     */
    private void initToolbar() {
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar_search);
        mSlider = new MainPager.MDrawerArrowDrawableToggle(this, this);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon((Drawable) mSlider);
        mSlider.setPosition(1);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }


    private void initRecyclerView() {
        Seach_ContentListlist = new ArrayList<>();

        bitmapUtils = HttpBitMapHelp.getBitmapUtils(this.getApplicationContext());

        seachDataRecyclerAdapter = new SeachDataRecyclerAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);


        recyclerView = (RecyclerView) findViewById(R.id.seach_pager_recyclerView);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(seachDataRecyclerAdapter);
        seachDataRecyclerAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<Seach_ContentList>() {
            @Override
            public void onItemClick(View view, Seach_ContentList item, boolean isLongClick) {
                if (!isLongClick) {
                    Navigator.launchDetailSeach(SearchPager.this, view, item, null);
                }
            }
        });

    }


    /**************************init区结束***************************************/
    /**
     * ***********************方法调用区**************************************
     */
    @Override
    public void onClick(View v) {  //搜索按钮点击

        doSearch();

    }

    private void doSearch() {
        rws.getSeachData(ppm.getSeachUrl(seach_pager_editText.getText().toString()), new FetchJSONDAO.SeachDataResultBack() {
            @Override
            public void seachResultWhat(boolean isSuccusful, SeachData seachData) {
                if (isSuccusful) {
                    Seach_ContentListlist = seachData.getContent_lists();

                    if(Seach_ContentListlist!=null){
                        seachDataRecyclerAdapter.updateList(Seach_ContentListlist);
                        Toast.makeText(SearchPager.this, "搜索成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(SearchPager.this, "没有搜索到需要的内容", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(SearchPager.this, "搜索失败,请重试", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**************************方法调用区结束***************************************/
    /**
     * ***********************内部类区**************************************
     */

    class SeachDataRecyclerAdapter extends BaseRecyclerAdapter<Seach_ContentList> {

        @Override
        public ThingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recylcer_item_newstype, parent, false);
            return new ThingHolder(view);
        }

        public class ThingHolder extends BaseRecyclerAdapter<Seach_ContentList>.ViewHolder {
            @InjectView(R.id.item_news_title)
            TextView titleTextView;
            @InjectView(R.id.item_news_ItemViewImg0)
            ImageView ItemViewImg;

            public ThingHolder(View itemView) {
                super(itemView);
            }

            public void populate(Seach_ContentList item) {
                titleTextView.setText(item.getTitle());

                if (item.getImgurl().length() > 0) {
                    bitmapUtils.display(ItemViewImg, item.getImgurl());
                }
            }
        }
    }
    /**************************内部类区结束***************************************/
}

